package com.jiai.cloud.community.controller;
import com.jiai.cloud.base.entity.RestResult;
import com.jiai.cloud.base.exception.ServiceException;
import com.jiai.cloud.base.tools.RestResultGenerator;
import com.jiai.cloud.base.util.IdGenerate;
import com.jiai.cloud.community.entity.TCatalog;
import com.jiai.cloud.community.entity.TResource;
import com.jiai.cloud.community.service.ITCatalogService;
import com.jiai.cloud.community.service.ITResourceService;
import com.jiai.cloud.utils.UploadUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/5/7.
 */
@RequestMapping("/api/v1/file")
@RestController
public class FileContoller {

    private Logger logger= LoggerFactory.getLogger(FileContoller.class);
    @Autowired
    private ITCatalogService itCatalogService;

    @Autowired
    private ITResourceService ITResourceService;
    @RequestMapping(value = "/upload/{catalogId}/{userId}",method = RequestMethod.POST)
    @ResponseBody
    public RestResult upload(@RequestParam("file") MultipartFile file,@PathVariable("catalogId") Integer catalogId,@PathVariable("userId") Integer userId){
      try {
          TCatalog tCatalog =itCatalogService.selectById(catalogId);
          String filePath="";
          if (tCatalog==null){
              filePath="\\"+userId;
          }else {
              filePath=userId+"\\"+tCatalog.getSaveCatalog();
          }
         String pate=UploadUtil.copyFile(file,filePath);
          TResource  tResource=new TResource();
          if(catalogId ==0){
              tResource.setCatalogId(0);
          }else{
              tResource.setCatalogId(catalogId);
          }
          tResource.setUserId(userId);
          tResource.setImgUrl(pate);
          Integer cunt=(int) file.getSize();
          tResource.setSize(cunt);
          tResource.setCreateTime(new Date());
          ITResourceService.insertOrUpdate(tResource);
         return RestResultGenerator.genSuccessResult(catalogId);
      }catch (Exception e){
          logger.error("文件上传失败!"+e.getMessage());
         return RestResultGenerator.genErrorResult("上传失败");
      }
    }

    @RequestMapping("/img")
    public void  image(HttpServletResponse response, String fileName,Integer width,Integer height){
        try{
            response.setContentType("image/png");
            String uploadPath= UploadUtil.getFilePath(fileName);
            File file=new File(uploadPath);
            FileInputStream inputStream = new FileInputStream(file);
            byte[] data = new byte[(int)file.length()];
            int length = inputStream.read(data);
            inputStream.close();
            OutputStream stream = response.getOutputStream();
            stream.write(data);
            stream.flush();
            stream.close();
        }catch (Exception e){
            logger.error("图片下载失败!error:[{}]",e.getMessage());
        }
    }
    @RequestMapping("/imgPath")
    public Object  imgPath(String imgPath)throws ServiceException{
        try {
            return RestResultGenerator.genSuccessResult(imgPath);
        }catch (Exception e) {
            throw new ServiceException("查询失败", e.getMessage());
        }
    }

}
